﻿//Eggtimer by Thomas Arvidsson, 2011-12-27

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

using System.Device.Location;


namespace EggTimer.Altitude
{
    public class AltitudeManager : INotifyPropertyChanged
    {
        #region Fields and propertys

        //Create a object to track the geo position
        protected GeoCoordinateWatcher Watcher { get; set; }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };
                
        private double _altitude;
        public double Altitude
        {
            get { return _altitude; }

            set 
            { 
                _altitude = value; 
                PropertyChanged(this, new PropertyChangedEventArgs("Altitude"));
            }
        }

        private bool gpsIsReady;
        public bool GpsIsReady 
        {
            get { return gpsIsReady; }
            set 
            {                
                gpsIsReady = value;
            } 
        }

        public bool AllowGps { get; set; }

        #endregion Fields and propertys

        //public event PropertyChangedEventHandler PropertyChanged;

        private void OnStatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            if (e.Status == GeoPositionStatus.Ready)
                GpsIsReady = true;

            else if (e.Status != GeoPositionStatus.Ready)
                GpsIsReady = false;
        }

        private void OnPositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            Altitude = e.Position.Location.Altitude;
        }

        public AltitudeManager()
        {
            GpsIsReady = false;
            AllowGps = false;

            Watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);

            Watcher.Start();

            Watcher.StatusChanged += OnStatusChanged;
            Watcher.PositionChanged += OnPositionChanged;  
        }

        
 
 
    

    }
}
